package com.emmmya.ocs.modules.system.security.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.emmmya.ocs.modules.system.annotation.OperateLog;
import com.emmmya.ocs.common.utils.ResultUtil;
import com.emmmya.ocs.common.vo.Result;
import com.emmmya.ocs.modules.system.security.entity.UrlFilter;
import com.emmmya.ocs.modules.system.security.service.DynamicPermissionService;
import com.emmmya.ocs.modules.system.security.vo.UrlFilterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 动态修改shiro的url配置
 * @author: 咕噜肉IT小店-黑医
 * @createDate: 2020-12-22
 * @version: 1.0
 */
@Controller
@RequestMapping("system/security")
public class SecurityController {
    @Autowired
    DynamicPermissionService dynamicPermissionService;

    @RequestMapping("/sysApi")
    public String sysApi(){
        return "admin2/system/sys-api";
    }


    @RequestMapping("/addApiUI")
    public String addApiUI(Model model, @RequestParam(value="id",required = false)Long id){
        if(id != null){
            UrlFilter urlFilter = dynamicPermissionService.get(id);
            model.addAttribute("urlFilter",urlFilter);
        }
        return "admin2/system/edit/api-add";
    }

    /**
     * 分页查询api列表
     */
    @RequestMapping("/getApiList")
    @ResponseBody
    public Result<Object> getApiList(UrlFilterVo vo){
        Page<UrlFilter> page = new Page<>(vo.getPage(),vo.getLimit());
        dynamicPermissionService.getListByPage(page,null);
        return ResultUtil.data(page);
    }

    /**
     * 动态刷新系统的api权限。
     */
    @RequestMapping("/refreshSystemApi")
    @ResponseBody
    @OperateLog(module ="系统URL权限管理",value="刷新系统本地权限")
    public Result<Object> refreshSystemApi(){
        try {
            dynamicPermissionService.updateSystemApi();
            return ResultUtil.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(e);
        }
    }

    /**
     * 根据id查询对应的api信息
     * @param id
     * @return
     */
    @RequestMapping("/queryApiById")
    @ResponseBody
    public Result<Object> queryApi(@RequestParam("id")Long id){
        UrlFilter urlFilter = dynamicPermissionService.get(id);
        if(urlFilter!=null){
            String name = urlFilter.getName();
            String s = urlFilter.getDefinition();
            return ResultUtil.success("当前的"+name+"状态："+("anon".equals(s)?"开放":"不开放"));
        }
        return ResultUtil.error("不存在id为"+id+"的接口");
    }

    /**
     * 这里修改的是yml文件里维护的。
     * @param name      api的url
     * @param status    true 表示 anon 开放， false 表示 authc 需要授权
     * @return
     */
    @RequestMapping("setApiStatus")
    @ResponseBody
    public Result<Object> updateApiStatus(@RequestParam("apiUrl")String name,@RequestParam("status")boolean status){
        try {
            Map<String,String> newMap = new HashMap<String,String>();

            newMap.put(name, status ? "anon" : "authc");
            dynamicPermissionService.updateOnePermission(newMap);
            return ResultUtil.success("修改成功");
        } catch (Exception e) {
            //e.printStackTrace();
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/addApi")
    @ResponseBody
    public Result<Object> addApi(@RequestBody UrlFilter urlFilter){
        try {
            UrlFilter save = dynamicPermissionService.save(urlFilter);
            return ResultUtil.data(save,"新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/updateApi")
    @ResponseBody
    public Result<Object> updateApi(@RequestBody UrlFilter urlFilter){
        try {
            dynamicPermissionService.update(urlFilter);
            return ResultUtil.success("更新成功！");
        } catch (Exception e) {
//            e.printStackTrace();
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/deleteApi")
    @ResponseBody
    public Result<Object> deleteApi(@RequestParam("id") Long id){
        dynamicPermissionService.deleteAndCheck(id);
        return ResultUtil.success("删除成功！");
    }


}
